package net.wanho.customerManage.controller;

import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.customerManage.po.BusinessSource;
import net.wanho.customerManage.po.Customer;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.customerManage.po.WorkingField;
import net.wanho.customerManage.query.CustomerQuery;
import net.wanho.customerManage.service.BusinessSourceService;
import net.wanho.customerManage.service.CustomerService;
import net.wanho.customerManage.service.LinkManService;
import net.wanho.customerManage.service.WorkingFieldService;
import net.wanho.customerManage.vo.CustomerVo;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/CustomerServlet")
public class CustomerServlet  extends HttpServlet{
	private CustomerService customerService = BeanFactory.getBean(CustomerService.class) ;
	private LinkManService linkManService = BeanFactory.getBean(LinkManService.class) ;
	private WorkingFieldService workingFieldService = BeanFactory.getBean(WorkingFieldService.class) ;
	private BusinessSourceService businessSourceService = BeanFactory.getBean(BusinessSourceService.class) ;
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	/*
	 * 新增
	 */
	protected void addCustomer(HttpServletRequest request,HttpServletResponse response) throws IOException {
		JSONObject obj=new JSONObject();
		obj.put("flag", 1);
		obj.put("msg", "添加成功");
		try {
			Customer customer = new Customer();
			BeanUtils.populate(customer,request.getParameterMap());
			Integer rs = customerService.insertCustomer(customer) ;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		response.getWriter().write(obj.toString());
	}
	
	/*
	 * 查询列表
	 */
	protected void getAllCustomer(HttpServletRequest request,HttpServletResponse response) {
		try {
			List<CustomerVo> customerVoList = customerService.selectAllCustomer();
			request.setAttribute("customerVoList",customerVoList);
			//2.查询所有 所属行业
			List<WorkingField> workingFieldList = workingFieldService.selectAllWorkingField();
			//3. 查询 客户来源
			List<BusinessSource> businessSourceList = businessSourceService.selectAllBusinessSource();
			request.setAttribute("workingFieldList",workingFieldList);
			request.setAttribute("businessSourceList",businessSourceList);
			request.getRequestDispatcher("/WEB-INF/view/customerManage/customer/index.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 查询列表
	 */
	protected void getAllZd(HttpServletRequest request,HttpServletResponse response) {
		try {
			//1. 查询所有联系人
			List<LinkMan> linkManList = linkManService.selectAllLinkMan();
			//2.查询所有 所属行业
			List<WorkingField> workingFieldList = workingFieldService.selectAllWorkingField();
			//3. 查询 客户来源
			List<BusinessSource> businessSourceList = businessSourceService.selectAllBusinessSource();
			request.setAttribute("linkManList",linkManList);
			request.setAttribute("workingFieldList",workingFieldList);
			request.setAttribute("businessSourceList",businessSourceList);
    		request.getRequestDispatcher("/WEB-INF/view/customerManage/customer/create.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 单个数据查询
	 */
	protected void selectByPrimaryKey(HttpServletRequest request,HttpServletResponse response) {
		try {

			String id = request.getParameter("type");
			//获得用户请求的参数:
			String customerId = request.getParameter("customerId");
			CustomerVo customerVo = customerService.selectByIdCustomer(Integer.parseInt(customerId));
			request.setAttribute("customerVo",customerVo);
			if(!"1".equals(id)){
				//1. 查询所有联系人
				List<LinkMan> linkManList = linkManService.selectAllLinkMan();
				//2.查询所有 所属行业
				List<WorkingField> workingFieldList = workingFieldService.selectAllWorkingField();
				//3. 查询 客户来源
				List<BusinessSource> businessSourceList = businessSourceService.selectAllBusinessSource();
				request.setAttribute("linkManList",linkManList);
				request.setAttribute("workingFieldList",workingFieldList);
				request.setAttribute("businessSourceList",businessSourceList);
				request.getRequestDispatcher("/WEB-INF/view/customerManage/customer/update.jsp").forward(request, response);
				return;
			}else{
				request.getRequestDispatcher("/WEB-INF/view/customerManage/customer/view.jsp").forward(request, response);

			}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 修改
	 */
	protected void updateCustomer(HttpServletRequest request,HttpServletResponse response) {

	}
	
	/*
	 * 搜索
	 */
	protected void getSomeCustomer(HttpServletRequest request,HttpServletResponse response) throws ParseException {
		try {
			CustomerQuery customerQuery = new CustomerQuery();
			BeanUtils.populate(customerQuery,request.getParameterMap());
			String customerName = URLDecoder.decode(customerQuery.getText(),"utf-8") ;
			customerQuery.setText(customerName);
			//2.查询所有 所属行业
			List<WorkingField> workingFieldList = workingFieldService.selectAllWorkingField();
			//3. 查询 客户来源
			List<BusinessSource> businessSourceList = businessSourceService.selectAllBusinessSource();
			request.setAttribute("workingFieldList",workingFieldList);
			request.setAttribute("businessSourceList",businessSourceList);
			List<CustomerVo> customerVoList = customerService.selectByCondCustomer(customerQuery);
			request.setAttribute("customerVoList",customerVoList);
			request.setAttribute("customerQuery",customerQuery);
			request.getRequestDispatcher("/WEB-INF/view/customerManage/customer/index.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
